<template>
  <div>
    <div class="image-container">
      <!--查询，新增，导出-->
      <div class="search-bar" style="display: flex;width: 500px;padding-bottom: 10px;">
        <!-- 搜索输入框 -->
        <el-input v-model="stuNumber" style="width: 250px" placeholder="请输入学号...."></el-input>
        <!-- 查询按钮 -->
        <el-button type="primary" @click="handleSearch" style="margin-left:10px">查询</el-button>
        <!-- 新增按钮 -->
        <el-button type="primary" @click="addScore">新增宿舍</el-button>
        <el-button type="primary" @click="exportExcel">导出Excel</el-button>
      </div>

      <!--表格-->
      <el-table border :data="tableData" style="width: 100%">
        <el-table-column prop="stuNumber" label="学号" width="180">
        </el-table-column>
        <el-table-column prop="name" label="姓名" width="180">
        </el-table-column>
        <el-table-column prop="couName" label="成绩" width="180">
        </el-table-column>
        <el-table-column prop="grade" label="成绩" width="180">
        </el-table-column>
        <el-table-column prop="point" label="学时" width="180">
        </el-table-column>
        <el-table-column prop="major" label="专业" width="180">
        </el-table-column>
        <!--操作-->
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <el-button type="primary" size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!--分页组件-->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[7, 10, 20, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
    />


    <!--对话框-->
    <el-dialog title="成绩信息" :visible.sync="dialogVisible" width="30%">
      <el-form :model="newScoreForm" label-width="80px">
        <el-form-item label="学号">
          <el-input v-model="newScoreForm.stuNumber"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="newScoreForm.name"></el-input>
        </el-form-item>
        <el-form-item label="成绩">
          <el-input v-model="newScoreForm.grade"></el-input>
        </el-form-item>
        <el-form-item label="成绩">
          <el-input v-model="newScoreForm.point"></el-input>
        </el-form-item>
        <el-form-item label="专业">
          <el-input v-model="newScoreForm.major"></el-input>
        </el-form-item>
        <el-form-item label="课程">
          <el-input v-model="newScoreForm.couName"></el-input>
        </el-form-item>
      </el-form>
      <el-button type="primary" @click="handleSubmit">提交</el-button>
      <el-button @click="dialogVisible = false">取消</el-button>
    </el-dialog>


  </div>


</template>
<script>
import {getScore, addScore,updateScore, deleteScore} from '@/api/ScoreApi';

export default {
  name: 'Grade',
  data() {
    return {
      tableData: [],// 初始化数据
      currentPage: 1,// 初始化当前页码
      total: 0,// 分页总数
      pageSize: 7,//  初始化每页显示数量,
      dialogVisible: false,
      newScoreForm: {// 新增宿舍表单数据
        stuNumber: '',
        name: '',
        grade: '',
        point: '',
        major: '',
        couName: '',
      },
      stuNumber:'' // 搜索输入框的值
    }
  },
  created() {
    this.fetchData(this.currentPage, this.pageSize,this.stuNumber);
  },
  methods: {
    fetchData(page, size, stuNumber = '') {
      const params = {
        page: page,
        pageSize: size,
        stuNumber: stuNumber
      };
      // 调用API获取数据
      getScore(params)
          .then(res => {
            if (res.data.code === 1) {
              console.log(res.data)
              this.tableData = res.data.data.records;
              this.total = res.data.data.total || 0;
            } else {
              console.error('获取成绩数据失败');
            }
          })
          .catch(error => {
            console.error('请求成绩错误:', error);
          });
    },
    // 导出
    exportExcel() {
    },

    // 查询
    handleSearch() {
      this.fetchData(this.currentPage, this.pageSize, this.stuNumber);
    },
    // 分页
    handleSizeChange(size) {
      this.pageSize = size;
      this.fetchData(this.currentPage, this.pageSize);
    },
    // 分页
    handleCurrentChange(page) {
      this.currentPage = page;
      this.fetchData(this.currentPage, this.pageSize);
    },
    // 新增
    addScore() {
      this.dialogVisible = true;
    },

    // 提交
    handleSubmit() {
      if (this.newScoreForm.stuNumber) {
        // 如果表单中存在学号stuNumber，则执行更新操作
        if(this.newScoreForm){
          updateScore(this.newScoreForm)
              .then((response) => {
                if (response.data.code === 1) {
                  this.$message.success("更新成功");
                  console.log(response.data)
                  this.dialogVisible = false;
                  this.fetchData(this.currentPage, this.pageSize);
                } else {
                  this.$message.error(response.data.msg);
                }
              }).catch(error => {
                console.error("修改成绩失败:", error);
              })
        }else {
          // 调用API添加宿舍
          addScore(this.newScoreForm)
              .then(res => {
                if (res.data.code === 1) {
                  console.log(res.data)
                  this.$message.success('添加成功');
                  this.dialogVisible = false;
                  // 清空表单数据
                  this.$refs.adminForm.resetFields();
                  // 刷新宿舍列表或重新请求数据
                }
              })
        }

      }

    },

    // 删除
    handleDelete(index, row) {
      this.$confirm('确定删除当前成绩吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        // 调用API删除宿舍
        deleteScore(row.stuNumber)
            .then(res => {
              if (res.data.code === 1) {
                this.$message.success('删除成功');
                this.fetchData(this.currentPage, this.pageSize); // 在删除成功后调用
              } else {
                this.$message.error(res.data.msg);
                this.$message.info("删除成绩失败")
              }
            })
      }).catch(error => {
        console.error('删除成绩失败:', error);
      })
    },

    // 编辑
    handleEdit(index, row) {
      this.newScoreForm = {...row};
      this.dialogVisible = true;

    },

  },

}

</script>
<style>

</style>